﻿using System;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FW;
namespace Kcadastro.adm
{
    public partial class precos_ed : System.Web.UI.Page
    {
        private fwlog log = (fwlog)ConfigurationManager.GetSection("fwlog");

        kcadDataContext db = new kcadDataContext(ConfigurationManager.ConnectionStrings["KcadConnectionString"].ToString());

        protected void Page_Load(object sender, EventArgs e)
        {

            masterPageKcad m = Master as masterPageKcad;
            m.idFerr = 6;
            if (!IsPostBack)
            {
                if (Request.QueryString["i"] != null)
                {
                    if (Request.QueryString["i"].ToString() != "")
                    {
                        tbi_serv.Text = Request.QueryString["i"].ToString();
                        carregar();
                    }
                }
                
            }
        }

        private void carregar()
        {
            tb_servico ts = db.tb_servicos.Single(t => t.PK_IDserv == Convert.ToInt16(tbi_serv.Text));
            if (db.tb_precos.Any(t => t.FK_IDserv == Convert.ToInt16(tbi_serv.Text)))
            {
                tb_preco tp = db.tb_precos.Single(t => t.FK_IDserv == Convert.ToInt16(tbi_serv.Text));

                tbi_prec.Text = tp.PK_IDorc.ToString();
                tb_descri.Text = ts.descri;
                tb_nome.Text = ts.nome;
                tb_qtdmin.Text = tp.RegMinimo.ToString();
                tb_valadd.Text = tp.valorporregistro.ToString();
                tb_valfranq.Text = tp.franquia.ToString();
                dd_tpps.SelectedIndex = dd_tpps.Items.IndexOf(dd_tpps.Items.FindByValue(ts.tpps.ToString()));
                cb_ativo.Checked = (bool)ts.ativo;
            }
        }

        protected void bt_gravar_Click(object sender, EventArgs e)
        {
            tb_servico ts = db.tb_servicos.Single(t => t.PK_IDserv == Convert.ToInt16(tbi_serv.Text));
            if (db.tb_precos.Any(t => t.FK_IDserv == Convert.ToInt16(tbi_serv.Text)))
            {
                tb_preco tp = db.tb_precos.Single(t => t.FK_IDserv == Convert.ToInt16(tbi_serv.Text));

                ts.ativo = cb_ativo.Checked;
                ts.nome = tb_nome.Text;
                ts.descri = tb_descri.Text;
                tp.franquia = Convert.ToDecimal(tb_valfranq.Text);
                tp.RegMinimo = Convert.ToInt16(tb_qtdmin.Text);
                tp.valorporregistro = Convert.ToDecimal(tb_valadd.Text);

                db.SubmitChanges();
            }
            else
            {
                tb_preco tbp = new tb_preco();
                ts.ativo = cb_ativo.Checked;
                ts.nome = tb_nome.Text;
                ts.descri = tb_descri.Text;
                tbp.FK_IDserv = Convert.ToInt16(tbi_serv.Text);
                tbp.franquia = Convert.ToDecimal(tb_valfranq.Text);
                tbp.RegMinimo = Convert.ToInt16(tb_qtdmin.Text);
                tbp.valorporregistro = Convert.ToDecimal(tb_valadd.Text);
                db.tb_precos.InsertOnSubmit(tbp);
                db.SubmitChanges();
            }
        }
    }
}